//
//  CYButton.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYButton.h"

@interface CYButton ()
@property (nonatomic, strong) UIColor *backColor_normal;
@property (nonatomic, strong) UIColor *backColor_select;

@end
@implementation CYButton

- (instancetype)initWithTitle:(NSString *)title titleColor:(UIColor *)color backgroundColor:(UIColor *)tintcolor cornerRadius:(CGFloat)radius{
    self = [super init];
    if (self) {
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:color forState:UIControlStateNormal];
        [self setBackgroundColor:tintcolor forstate:UIControlStateNormal];
        self.layer.cornerRadius = radius;
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title titleColor:(UIColor *)color titleSize:(CGFloat)size Image:(NSString *)image{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self) {
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:color forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        self.titleLabel.font = (size != 0)?[UIFont systemFontOfSize:size]:[UIFont systemFontOfSize:17];
        self.titleEdgeInsets = UIEdgeInsetsMake(self.imageView.bounds.size.height, - self.imageView.bounds.size.width, 0.0, 0.0);
        self.imageEdgeInsets = UIEdgeInsetsMake(-self.titleLabel.bounds.size.height,0, 0.0, -self.titleLabel.bounds.size.width);
   
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title titlesize:(CGFloat)size cornerRadius:(CGFloat)radius{
    self = [super init];
    if (self) {
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.layer.masksToBounds = (radius > 0.0)?YES:NO;
        self.layer.cornerRadius = radius;
        self.layer.borderColor = (radius > 0.0)?[UIColor blackColor].CGColor:[UIColor clearColor].CGColor;
        self.layer.borderWidth = (radius > 0.0)?0.5:0.0;
        self.titleLabel.font = [UIFont systemFontOfSize:size];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

- (instancetype)initWithImage:(NSString *)image cornerRadius:(CGFloat)radius{
    self = [super init];
    if (self) {
        [self setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = (radius > 0)?radius:0.0;
        self.layer.borderWidth = 0.5;
        self.layer.borderColor = [UIColor clearColor].CGColor;
    }
    
    return self;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor forstate:(UIControlState)state{
    if (state == UIControlStateSelected)
        _backColor_select = backgroundColor;
    else if (state == UIControlStateNormal){
        _backColor_normal = backgroundColor;
        [self setBackgroundColor:backgroundColor];
    }
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    if (selected && _backColor_select)
        [self setBackgroundColor:_backColor_select];
    else if (!selected && _backColor_normal)
        [self setBackgroundColor:_backColor_normal];
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state{
    [super setTitle:title forState:state];
    if (_imagePosition != 0)
        [self setupImagePosition];
}

- (void)setImagePosition:(CYButtonImageViewPosition)imagePosition{
    if (_imagePosition != imagePosition) {
        _imagePosition = imagePosition;
        [self setupImagePosition];
    }
}
- (void)setupImagePosition{
    if (_imagePosition == RightPosition) {
        self.titleEdgeInsets = UIEdgeInsetsMake(0, - self.imageView.bounds.size.width, 0.0, self.imageView.bounds.size.width);
        self.imageEdgeInsets = UIEdgeInsetsMake(0, self.titleLabel.bounds.size.width, 0.0, -self.titleLabel.bounds.size.width);
    }
}
//- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event{
//
////    DeLog(@"self %f   sendaction  %@%f   imageview %f",self.bounds.size.width,self.titleLabel.text,self.titleLabel.bounds.size.width, self.imageView.bounds.size.width);
////    if (!target)
////        [super sendAction:action to:target forEvent:event];
////    else{
//        //        DeLog(@"action %@   target %@  event %@",action, target, event);
//        [super sendAction:action to:target forEvent:event];
//
////    }
//}
@end
